package com.mytrip.webapp.interceptor;

import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.mytrip.util.LoginUtil;
import com.mytrip.webapp.filter.CtxSessionBag;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.config.entities.ResultConfig;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class BackendLoginInterceptor extends AbstractInterceptor {
	private final Log log = LogFactory.getLog(BackendLoginInterceptor.class);
	private static final long serialVersionUID = -7893720415150752485L;

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		if(LoginUtil.validateCookie()==false) {
			return "backendLogin";
		}
		Map<String, ResultConfig> m = invocation.getProxy().getConfig().getResults();
		if (m != null) {
			for (Object k : m.keySet()) {
				try {
					ResultConfig rc = m.get(k);
					if (rc.getLocation() != null && "action".equals(rc.getLocation().getDescription())&& rc.getClassName()
							.equals(org.apache.struts2.dispatcher.StreamResult.class.getName())) {// 本地变量，且为流结果
						log.warn("exportoperator=" + CtxSessionBag.getSessionBag().getBackOperator().getUserName() + ",actionName="
								+ invocation.getProxy().getConfig().getClassName()+ ",methodName="
								+ invocation.getProxy().getConfig().getMethodName() + ",resultName=" + rc.getName());
					}
				} catch (Exception e) {
					log.error("::",e);
				}
			}
		}
		return invocation.invoke();
	}
}
